<?php
	include_once $_SERVER["DOCUMENT_ROOT"].'/service/entite/concert/ConcertEntite.php';
	include_once $_SERVER["DOCUMENT_ROOT"].'/service/entite/albumphoto/AlbumPhotoService.php';
	include_once $_SERVER["DOCUMENT_ROOT"].'/persistence/concert/ConcertDAO.php';

	class ConcertService {
		
		private function ConcertService() {
			//évite la construction
		}
		
		public static function createConcert() {
			return new ConcertEntite();
		}
		
		public static function getConcertForModification($id) {
			return ConcertDAO::getConcertByID($id);
		}
		
		public static function getConcertByID($id) {
			return ConcertDAO::getConcertByID($id);
		}
		
		public static function supprimerConcert($id) {
			$concertEntite = ConcertDAO::getConcertByID($id);
			AlbumPhotoService::supprimerAlbumPhoto($concertEntite->albumPhoto->id);
			return ConcertDAO::delete($id);
		}
		
		public static function save(ConcertEntite $concertEntite) {
			$isCreation = $concertEntite->id == Entite::getUnpersistedId();
			if ($isCreation) {
				//si c'est une création, on crée l'album correspondant
				$albumPhotoEntite = AlbumPhotoService::createAlbumPhoto();
				$concertEntite->albumPhoto = $albumPhotoEntite;
			} else {
				$albumPhotoEntite = $concertEntite->albumPhoto;
			}
			ConcertService::updateNomDateAlbumPhoto($concertEntite, $albumPhotoEntite);
			$result = ConcertDAO::save($concertEntite);
			return $result;
		}
		
		public static function linkWithConcertIfExists($idConcert, $idAlbumPhoto) {
			$concertEntite = ConcertDAO::getConcertById($idConcert);
			if ($concertEntite == null) {
				return;
			}
			$albumPhotoEntite = AlbumPhotoService::getAlbumPhotoByID($idConcert);
			$concertEntite->albumPhoto = $albumPhotoEntite;
			ConcertService::updateNomDateAlbumPhoto($concertEntite, $albumPhotoEntite);
			return ConcertDAO::save($concertEntite);
		}
		
		public static function updateNomDateAlbumPhoto(ConcertEntite $concertEntite, AlbumPhotoEntite $albumPhotoEntite) {
			AlbumPhotoDAO::loadCouverture($albumPhotoEntite);
			$albumPhotoEntite->nom = ConcertService::getPresentationConcert($concertEntite);
			$albumPhotoEntite->date = $concertEntite->date;
			AlbumPhotoService::save($albumPhotoEntite);
		}
		
		public static function getListeConcert() {
			return ConcertDAO::getListeConcert();
		}
		
		public static function getProchainConcert() {
			return ConcertDAO::getProchainConcert();
		}
		
		public static function getListeConcertAVenir() {
			return ConcertDAO::getListeConcertAVenir();
		}
		public static function getListeConcertPasse() {
			return ConcertDAO::getListeConcertPasse();
		}
		
		public static function getPresentationConcert(ConcertEntite $concertEntite) {
			return "Concert du $concertEntite->date ($concertEntite->lieu)";
		}
		
		public static function getConcertLieAAlbum($idAlbumPhoto) {
			return ConcertDAO::getConcertLieAAlbum($idAlbumPhoto);
		}
	}
?>